package com.acerola.yuan_back.controller;


import com.acerola.yuan_back.pojo.MailVo;
import com.acerola.yuan_back.utils.MailUtils;
import com.acerola.yuan_back.utils.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.mail.MailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.acerola.yuan_back.utils.TimeUtils.dateDiff;

/**
 * @program: yuan_back
 * @description: 邮件与短信控制类
 * @author: Acerola
 * @create: 2022-10-10 01:13
 **/
@Api(tags = "邮件与短信控制类")
@RestController
public class MessageController {

    @Autowired
    private MailUtils mailUtils;


    /**
     * cron 表达式在线生成地址：https://cron.qqe2.com/
     */
    @ApiOperation("定时邮件任务")
    @Scheduled(cron = "0 01 18 * * ?")//每天18点01执行一次
    public void send() {
        System.out.println("开始执行");
        String format = "yyyy-MM-dd HH:mm:ss";
        String start = "2022-06-15 18:00:00";
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        int day = (int) dateDiff(start, formatter.format(now), format, "d");
//         if (day % 10 == 0) {
        MailVo mailMo = new MailVo();
        mailMo.setId("1314");
        mailMo.setTo("2502513332@qq.com,1463942091@qq.com,jialiang.mo@outlook.com");
//            mailMo.setTo("2502513332@qq.com");
        mailMo.setSubject("Acerola");
        Map<String, Object> mapMo = new HashMap<>();
        mapMo.put("day", day);
        mailMo.setAttachment(mapMo);
        mailUtils.sendTemplateMail(mailMo);
//        }
    }
}
